【JavaEE】分页小结          返回主页

本文对JavaEE的分页进行小结

最终效果(每个页面三条)

实现思路

使用数据库提供的分段查询语句及关键字进行分段获取,在servlet中进行相关计算并输出对应数量的数据条目;

代码呈现

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String pagenoString = request.getParameter("current_pageno");

    int current_pageno = 1;

    try {
        current_pageno = Integer.parseInt(pagenoString);
    } catch (Exception e) {
        e.printStackTrace();
    }

    UserinfoDao userinfoDao = new UserinfoDaoImpl();
    //获取用户信息数量
    int userinfoRowsCount = userinfoDao.getUserinfoCount();
    //最大页数能被信息总数整除则最大页数为数量除以每一页条数否则为商加一
    int max_page = (userinfoRowsCount % PageBean.USERINFOLIST_PER_ROWS == 0)
            ? (userinfoRowsCount / PageBean.USERINFOLIST_PER_ROWS)
            : (userinfoRowsCount / PageBean.USERINFOLIST_PER_ROWS + 1);

    if (current_pageno < 1) {
        current_pageno = 1;
    }

    if (current_pageno > max_page) {
        current_pageno = max_page;
    }

    List<Userinfo> userinfoList = userinfoDao.fetchUserinfoList(current_pageno);

    PageBean userinfo_pageBean = new PageBean();
    userinfo_pageBean.setCurrent_pageno(current_pageno);
    userinfo_pageBean.setMax_page(max_page);

    request.setAttribute("userinfo_pageBean", userinfo_pageBean);
    request.setAttribute("userinfoList", userinfoList);
    request.getRequestDispatcher("userinfoList.jsp").forward(request, response);
}

翻页按钮

<tr>
        <td colspan="4"><a href="UserinfoListServlet?current_pageno=1">首页</a>
            <a
            href="UserinfoListServlet?current_pageno=<%=userinfo_pageBean.getCurrent_pageno() - 1%>">上一页</a>
            <a
            href="UserinfoListServlet?current_pageno=<%=userinfo_pageBean.getCurrent_pageno() + 1%>">下一页</a>
            <a
            href="UserinfoListServlet?current_pageno=<%=userinfo_pageBean.getMax_page()%>">尾页</a></td>
    </tr>

第一页为当前页1,以后每往前翻一页为当前页减一往后为加一